package cn.wonderland.basic.service.impl;

import cn.wonderland.basic.mapper.BaseMapper;
import cn.wonderland.basic.query.BaseQuery;
import cn.wonderland.basic.service.BaseService;
import cn.wonderland.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // 查询条数
        Integer totals = baseMapper.queryByCount(query);
        if (totals == null || totals == 0) {
            return new PageList<T>(0, new ArrayList<>());
        }
        // 查询页数
        List<T> rows = baseMapper.queryByPage(query);
        return new PageList<>(totals,rows);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }
}
